package cn.lh.order.service.impl;

import cn.lh.basic.query.BaseQuery;
import cn.lh.basic.util.LoginContext;
import cn.lh.basic.util.Page;
import cn.lh.order.domain.OrderPetAcquisition;
import cn.lh.order.dto.ConfirmOrderDto;
import cn.lh.order.mapper.OrderPetAcquisitionMapper;
import cn.lh.order.query.OrderPetAcquisitionQuery;
import cn.lh.order.service.IOrderPetAcquisitionService;
import cn.lh.basic.service.impl.BaseServiceImpl;
import cn.lh.org.domain.Employee;
import cn.lh.org.domain.Shop;
import cn.lh.org.exception.BusinessException;
import cn.lh.org.mapper.EmployeeMapper;
import cn.lh.org.mapper.ShopMapper;
import cn.lh.pet.domain.Pet;
import cn.lh.pet.domain.SearchMasterMsg;
import cn.lh.pet.mapper.PetMapper;
import cn.lh.pet.mapper.SearchMasterMsgMapper;
import cn.lh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    PetMapper petMapper;

    public Page<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest req) {
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        Long shop_id = employee.getShop_id();
        if (shop_id != null) {
            query.setShopId(shop_id);
            Shop shop = shopMapper.loadById(shop_id);
            Long admin_id = shop.getAdmin_id();
            if (!admin_id.equals(employee.getId())) {
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(ConfirmOrderDto dto) {
        Long orderId = dto.getId();
        Integer payType = dto.getPayType();
        BigDecimal money = dto.getMoney();

        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        if (order.getState().equals(1)) {
            throw new BusinessException("重复操作");
        }
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理完成，交易成功");
        searchMasterMsgMapper.update(msg);
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void del(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        order.setState(0);
        order.setPrice(null);
        order.setPaytype(null);
        orderPetAcquisitionMapper.update(order);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        petMapper.remove(order.getPetId());
    }

}
